package com.shuyu.gsyvideoplayer.videosimple;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.FrameLayout;

import com.shuyu.gsyvideoplayer.GSYTextureView;
import com.shuyu.gsyvideoplayer.SimpleGSYTextureView;
import com.shuyu.gsyvideoplayer.SimpleGSYVideoManager;
import com.shuyu.gsyvideoplayer.listener.GSYMediaPlayerListener;
import com.shuyu.gsyvideoplayer.listener.VideoAllCallBack;
import com.shuyu.gsyvideoplayer.utils.CommonUtil;
import com.shuyu.gsyvideoplayer.utils.SimpleOrientationUtils;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import tv.danmaku.ijk.media.player.IjkMediaPlayer;

/**
 * 简洁基础播放
 * Created by shuyu on 2016/11/17.
 */

public abstract class SimpleGSYBaseVideoPlayer extends FrameLayout implements GSYMediaPlayerListener {


    protected static long CLICK_QUIT_FULLSCREEN_TIME = 0;


//    protected boolean mHideKey = false;//是否隐藏虚拟按键

    protected boolean mCache = true;//是否播边边缓冲

//    private boolean mShowFullAnimation = true;//是否使用全屏动画效果

    protected boolean mNeedShowWifiTip = true; //是否需要显示流量提示


    protected int mCurrentState = -1; //当前的播放状态

    protected int mRotate = 0; //针对某些视频的旋转信息做了旋转处理

    protected float mSpeed = 1;//播放速度，只支持6.0以上

    protected boolean mRotateViewAuto = false; //是否自动旋转


    protected boolean mLockLand = false;//当前全屏是否锁定全屏

    protected boolean mLooping = false;//循环

    protected boolean mHadPlay = false;//是否播放过

    protected boolean mCacheFile = false; //是否是缓存的文件


    protected Context mContext;

    protected String mOriginUrl; //原来的url

    protected String mUrl; //转化后的URL

    protected Object[] mObjects;

    protected File mCachePath;

   protected ViewGroup mTextureViewContainer; //渲染控件父类


    protected VideoAllCallBack mVideoAllCallBack;

    protected Map<String, String> mMapHeadData = new HashMap<>();

    protected SimpleGSYTextureView mSimpleTextureView;

    protected SimpleOrientationUtils mOrientationUtils; //旋转工具类


    /**
     * 1.5.0开始加入，如果需要不同布局区分功能，需要重载
     */
    public SimpleGSYBaseVideoPlayer(Context context, Boolean fullFlag) {
        super(context);
    }

    public SimpleGSYBaseVideoPlayer(Context context) {
        super(context);
    }

    public SimpleGSYBaseVideoPlayer(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SimpleGSYBaseVideoPlayer(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    private ViewGroup getViewGroup() {
        return (ViewGroup) (CommonUtil.scanForActivity(getContext())).findViewById(Window.ID_ANDROID_CONTENT);
    }

    /**
     * 移除没用的
     */
    private void removeVideo(ViewGroup vp, int id) {
        View old = vp.findViewById(id);
        if (old != null) {
            if (old.getParent() != null) {
                ViewGroup viewGroup = (ViewGroup) old.getParent();
                vp.removeView(viewGroup);
            }
        }
    }


    /**
     * 设置播放URL
     *
     * @param url
     * @param cacheWithPlay 是否边播边缓存
     * @param objects
     * @return
     */
    public abstract boolean setUp(String url, boolean cacheWithPlay, File cachePath, Object... objects);

    /**
     * 设置播放URL
     *
     * @param url
     * @param cacheWithPlay 是否边播边缓存
     * @param mapHeadData
     * @param objects
     * @return
     */

    public abstract boolean setUp(String url, boolean cacheWithPlay, File cachePath, Map<String, String> mapHeadData, Object... objects);

    /**
     * 设置播放显示状态
     *
     * @param state
     */
    protected abstract void setStateAndUi(int state);

    /**
     * 添加播放的view
     */
    protected abstract void addTextureView();




    public boolean isLooping() {
        return mLooping;
    }

    /**
     * 设置循环
     */
    public void setLooping(boolean looping) {
        this.mLooping = looping;
    }


    /**
     * 设置播放过程中的回调
     *
     * @param mVideoAllCallBack
     */
    public void setVideoAllCallBack(VideoAllCallBack mVideoAllCallBack) {
        this.mVideoAllCallBack = mVideoAllCallBack;
    }


    public boolean isRotateViewAuto() {
        return mRotateViewAuto;
    }

    /**
     * 是否开启自动旋转
     */
    public void setRotateViewAuto(boolean rotateViewAuto) {
        this.mRotateViewAuto = rotateViewAuto;
        if (mOrientationUtils != null) {
            mOrientationUtils.setEnable(rotateViewAuto);
        }
    }

    public boolean isLockLand() {
        return mLockLand;
    }

    /**
     * 一全屏就锁屏横屏，默认false竖屏，可配合setRotateViewAuto使用
     */
    public void setLockLand(boolean lockLand) {
        this.mLockLand = lockLand;
    }


    public float getSpeed() {
        return mSpeed;
    }

    /**
     * 播放速度
     */
    public void setSpeed(float speed) {
        this.mSpeed = speed;
        if (SimpleGSYVideoManager.instance().getMediaPlayer() != null
                && SimpleGSYVideoManager.instance().getMediaPlayer() instanceof IjkMediaPlayer) {
            if (speed != 1 && speed > 0) {
                ((IjkMediaPlayer) SimpleGSYVideoManager.instance().getMediaPlayer()).setSpeed(speed);
            }
        }
    }

//    public boolean isHideKey() {
//        return mHideKey;
//    }
//
//    /**
//     * 全屏隐藏虚拟按键，默认打开
//     */
//    public void setHideKey(boolean hideKey) {
//        this.mHideKey = hideKey;
//    }

    public boolean isNeedShowWifiTip() {
        return mNeedShowWifiTip;
    }


    /**
     * 是否需要显示流量提示,默认true
     */
    public void setNeedShowWifiTip(boolean needShowWifiTip) {
        this.mNeedShowWifiTip = needShowWifiTip;
    }

}
